﻿#include "VAOneXMLAdaptorAcousticsNCTRegularTrimLayer.h"

#include "FITKAcousticsPhysics/FITKAcousticsNCTRegularTrimLayer.h"

#include <QDebug>

namespace IO
{

    bool VAOneXMLAdaptorAcousticsNCTRegularTrimLayer::adaptR()
    {
        return readRegularTrimLayer();
    }

    bool VAOneXMLAdaptorAcousticsNCTRegularTrimLayer::adaptW()
    {
        return writeRegularTrimLayer();
    }

    bool VAOneXMLAdaptorAcousticsNCTRegularTrimLayer::readRegularTrimLayer()
    {
        if (!_node)// 判断节点是否存在
        {
            return false;
        }

        bool b = true;
        //转换类型
        Acoustics::FITKAcousticsNCTRegularTrimLayer* layerData = dynamic_cast<Acoustics::FITKAcousticsNCTRegularTrimLayer*>(_dataObj);

        if(!layerData)
        {
            return false;
        }

        double lossFactor = readDoubleAttribute(_node, "loss_factor", &b);

        if(b)
        {
            layerData->setLossFactor(lossFactor);
        }

        double thickness = readDoubleAttribute(_node, "thickness", &b);

        if(b)
        {
            layerData->setThickness(thickness);
        }

        bool isReadSolidMaterialValid = true;

        QString solidMaterial = readStrAttribute(_node, "solid_material", &isReadSolidMaterialValid);

        if(isReadSolidMaterialValid)
        {
            int matId = getMaterialObjectIdByName(solidMaterial);
            layerData->setSolidMaterialID(matId);
        }
//        else
//        {
//            //读材料读取异常
//            consoleMessage(3,"Read SolidMaterial Error" + solidMaterial);
//        }

        bool isReadFluidMaterialValid = true;
        QString fluidMaterial = readStrAttribute(_node, "fluid", &isReadFluidMaterialValid);


        if(isReadFluidMaterialValid)
        {
            int matId = getMaterialObjectIdByName(fluidMaterial);
            layerData->setFluidMaterialID(matId);
        }
//        else
//        {
//            //读材料读取异常
//            consoleMessage(3,"Read FluidMaterial Error" + fluidMaterial);
//        }

        layerData->initType();

        Acoustics::FITKAcousticsNCTAbstractLayObj::NCTLayUpType layerType =layerData->getNCTLayUpType();

        if(layerType == Acoustics::FITKAcousticsNCTAbstractLayObj::NCTLayUpType::Gap)
        {
            if(isReadFluidMaterialValid == false)
            {
                consoleMessage(3,"Read FluidMaterial Error" + fluidMaterial);
            }
        }
        else if(layerType == Acoustics::FITKAcousticsNCTAbstractLayObj::NCTLayUpType::Panel)
        {
            if(isReadSolidMaterialValid == false)
            {
                consoleMessage(3,"Read SolidMaterial Error" + solidMaterial);
            }
        }
        else if(layerType == Acoustics::FITKAcousticsNCTAbstractLayObj::NCTLayUpType::Foam ||
                layerType == Acoustics::FITKAcousticsNCTAbstractLayObj::NCTLayUpType::Fiber)
        {
            if(isReadSolidMaterialValid == false)
            {
                consoleMessage(3,"Read SolidMaterial Error" + solidMaterial);
            }

            if(isReadFluidMaterialValid == false)
            {
                consoleMessage(3,"Read FluidMaterial Error" + fluidMaterial);
            }
        }

        return true;
    }

    bool VAOneXMLAdaptorAcousticsNCTRegularTrimLayer::writeRegularTrimLayer()
    {
        if (!_node)// 判断节点是否存在
        {
            return false;
        }

        //转换类型
        Acoustics::FITKAcousticsNCTRegularTrimLayer* layerData = dynamic_cast<Acoustics::FITKAcousticsNCTRegularTrimLayer*>(_dataObj);

        if(!layerData)
        {
            return false;
        }

        //创建常规板节点
        rapidxml::xml_node<char>* regularTrimLayerNode = createNodeElement(_node, "RegularTrimLayer");

        if(!regularTrimLayerNode)
        {
            return false;
        }

        //写如属性值
        //写厚度
        writeAttribute(regularTrimLayerNode, "thickness", layerData->getThickness());
        //写流体材料
        writeAttribute(regularTrimLayerNode, "fluid", getMaterialObjectNameById(layerData->getFluidMaterialID()));
        //损失因子
        writeAttribute(regularTrimLayerNode, "loss_factor", layerData->getLossFactor());
        //固体材料
        writeAttribute(regularTrimLayerNode, "solid_material", getMaterialObjectNameById(layerData->getSolidMaterialID()));
        return true;
    }

}
